/*
 * Copyright (c) 2011 Metrum Research Group LLC. All Rights Reserved.
 *
 * You may use this software under the terms of GPL v3 (http://www.gnu.org/licenses/gpl.html).
 *
 * METRUM RESEARCH GROUP LLC MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. METRUM RESEARCH GROUP LLC SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package sumo;
import java.io.ByteArrayOutputStream;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import com.sshtools.j2ssh.SshClient;
public class SumoSessionListener implements HttpSessionListener {

	/* Session Creation Event */
	public void sessionCreated(HttpSessionEvent se) {
		System.out.println("Sumo Session was started...");
	}

	/* Session Invalidation Event */
	public void sessionDestroyed(HttpSessionEvent se) {
		// Here you want to do your cleanup stuff
		System.out.println("Sumo Session was destroyed, cleaning up...");
		HttpSession session = se.getSession();

		// Here we actually have to kill the threads running IOUtils
		//ByteArrayOutputStream baos = (ByteArrayOutputStream) session.getAttribute("Output");
		SshClient ssh = (SshClient) session.getAttribute("connection");
		// or maybe ...
		//SessionChannelClient ssh = (SessionChannelClient) session.getAttribute("SESSION");
		/*try {
			if (baos != null)
				baos.close(); //does nothing
		} catch (Exception e1) {
			e1.printStackTrace();
		}*/

		try {
			if (ssh != null && ssh.isConnected())
				ssh.disconnect();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
}